/* $Revision: $ $Date: $
 * 
 * Copyright (C) 2009 Robert Stephan
 * 
 * This file is part of *** CaCaO - Card Catalogs Online ***
 *
 * CaCaO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CaCao is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cacao.client.ui;

import cacao.client.CaCaO;
import cacao.client.CaCaOConstants;
import cacao.client.data.ItemDetailsObject;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;



public class EditCardCmp extends Composite implements CaCaOComposite{
	private CaCaO application;
	private PushButton btnStore;
	private Grid grid;
	private TextBox tbSignature;
	private TextArea taDescription;
	private CheckBox cbNoLoan;

	public EditCardCmp(CaCaO app){ 
		application=app;
		// Grids must be sized explicitly, though they can be resized later.
		grid = new Grid(4,2);
		// Put some values in the grid cells.
		tbSignature = new TextBox();
		tbSignature.setWidth("500px");
		taDescription = new TextArea(); 
		taDescription.setWidth("500px");
		btnStore = new PushButton(application.getMessages().editCardSave());
		cbNoLoan = new CheckBox(application.getMessages().editCardNoLoan());
		btnStore.setWidth("100px");
		btnStore.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent evt) {
				ItemDetailsObject data = new ItemDetailsObject();
				data.setId(application.getCurrentItem().getId());
				data.setShelfmark(tbSignature.getText());
				data.setDescription(taDescription.getText());
				data.setNoloan(cbNoLoan.getValue());

				AsyncCallback<String> callback2 = new AsyncCallback<String>(){
					public void onSuccess(String result2){
						if(result2!=null){
							Window.alert(application.getMessages().editCardFailureNotSaved(result2));
						}
					}
					public void onFailure(Throwable caught){
						Window.alert(application.getMessages().editCardFailureDisplay());
					}
				};
				application.getDataService().insertOrUpdateItemDetails(data, callback2);
			}
		});
		initWidget(grid);
	}

	public void updateUI(){
		AsyncCallback<String> callback = new AsyncCallback<String>(){
			public void onSuccess(String result){
				if(result ==null){ 
					//read only mode
					grid.setHeight("80px");
					grid.setText(0,0, "");
					grid.setText(1,0, "");
					grid.setText(2,0, "");
					grid.setHTML(2,1, "");
					grid.setText(3,0, "");
					grid.setText(3,1, "");
					grid.setHTML(0,1, "");
					grid.setHTML(1,1, "");

					ItemDetailsObject currentItem = application.getCurrentItem();
					if(currentItem!=null){
						String shelfmark = currentItem.getShelfmark();
						if(shelfmark.length()>0){
							grid.setHTML(0,1, application.getMessages().editCardAllreadyInCatalog(
									application.getProperties().get("CaCaO.client.opacsearchurl")+currentItem.getShelfmarkForURL(), shelfmark));
						}
						String descr = currentItem.getDescription();
						if(descr.length()>0){
							grid.setHTML(1,1, descr);
						}
						if(currentItem.isNoLoan()){
							grid.setHTML(2,1, application.getMessages().editCardNoLoanMessage());
						}
					}
				}
				else{
					//edit mode
					Window.enableScrolling(true);
					grid.setHeight("120px");
					grid.setText(0,0,application.getMessages().editCardShelfmark()+":");
					tbSignature.setText("");
					grid.setWidget(0,1, tbSignature);
					grid.setText(1,0, application.getMessages().editCardDescription()+":");
					grid.setWidget(1,1, taDescription);
					taDescription.setText("");
					grid.setText(2,0,"");
					grid.setWidget(3,1,btnStore);
					cbNoLoan.setValue(false, false);
					grid.setWidget(2,1, cbNoLoan);
					ItemDetailsObject currentItem = application.getCurrentItem();
					if(currentItem!=null){
						tbSignature.setText(currentItem.getShelfmark());
						taDescription.setText(currentItem.getDescription());
						cbNoLoan.setValue(currentItem.isNoLoan(), false);
					}
				}

			}
			public void onFailure(Throwable caught){
				Window.alert(application.getMessages().editCardFailureDisplay());
			}
		};
		application.getSessionService().getAttributeAsString(CaCaOConstants.SESSIONATTR_EDITOR_STRING, callback);
	}

}
